package com.edu.service.user.impl;


import com.edu.entity.user.EduUser;
import com.edu.mapper.user.EduUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomUserService implements UserDetailsService {
    @Autowired
    private EduUserMapper eduUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username){
//        只要求用户查询出实际的用户信息。具体的密码验证工作，交由spring security完成
        EduUser user=eduUserMapper.findUserByName(username);
        System.out.println("ddd:"+user);
        System.out.println(user);
        if (user!=null) {
            List<GrantedAuthority> authorities = new ArrayList<>();//授权，添加当前用户的角色
            return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), authorities);
        }else {
            throw new UsernameNotFoundException("admin:"+username+"do not exits!");
        }
    }
//    自定义用户登录实现层

}
